package com.cya.cyaproducts;

import java.util.ArrayList;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.cya.imageloader.ImageLoader;

public class ItemAdapter extends ArrayAdapter<Item>  {

    private final Context context;
    private final int rowResourceId;
    public ArrayList<Item> listProducts;
    public ImageLoader imgLoader;
    

    public ItemAdapter(Context context, int textViewResourceId, ArrayList<Item> products) {
    	super(context, textViewResourceId, products);
        this.context = context;
        this.listProducts = products;
        this.rowResourceId = textViewResourceId;
        
	     // ImageLoader class instance
	     imgLoader = new ImageLoader(context);
    }
    
    public void add(Item item_add)
    {
    	this.listProducts.add(item_add);
    	notifyDataSetChanged();
    }
    
    public int getIdItemByPosition(int position){
    	Item item_return = this.listProducts.get(position);
    	return item_return.Id;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
    	
    	// Loader image - will be shown before loading image
        int loader = R.drawable.loader;

        LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);

        View rowView = inflater.inflate(rowResourceId, parent, false);
        ImageView imageView = (ImageView) rowView.findViewById(R.id.imageView);
        TextView textView = (TextView) rowView.findViewById(R.id.textView);
        TextView descView = (TextView) rowView.findViewById(R.id.descView);
        
        TextView likeView = (TextView) rowView.findViewById(R.id.textIconLike);
        TextView downloadView = (TextView) rowView.findViewById(R.id.textIconDownload);
        TextView commentView = (TextView) rowView.findViewById(R.id.textIconComment);

        Item itemOnList = listProducts.get(position);

        String imageFile = itemOnList.IconFile;

        textView.setText(itemOnList.Name);
        descView.setText(itemOnList.Desc);
        likeView.setText(itemOnList.AmountLike);
        downloadView.setText(itemOnList.AmountDownload);
        commentView.setText(itemOnList.AmountComment);
        
        imgLoader.DisplayImage(imageFile, loader, imageView);
        return rowView;
    }

}
